<template>
  <el-dialog v-model="dialogFormVisible" title="请输入员工信息" @close="closeFn">
    <el-form :model="form">
      <el-form-item label="姓名" :label-width="formLabelWidth">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="手机号" :label-width="formLabelWidth">
        <el-input v-model="form.tel" :readonly="type===1?true:false" autocomplete="off" />
      </el-form-item>
      <el-form-item label="权限" :label-width="formLabelWidth">
        <el-select v-model="form.power" class="m-2" placeholder="Select">
          <el-option
            label="超管"
            :value="0"
          />
          <el-option
            label="经理"
            :value="1"
          />
          <el-option
            label="员工"
            :value="2"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门" :label-width="formLabelWidth">
        <el-select v-model="form.tId" class="m-2" placeholder="Select">
          <el-option
            v-for="item in team"
            :key="item.tId"
            :label="item.tName"
            :value="item.tId"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitFn">
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive, ref, watch, watchEffect } from 'vue';
import { getTeam } from '../../api';
import { useUserStore } from "../../storesPinia/user.js";
const userStore = useUserStore()
// 接收父组件传入的属性和自定义事件
const props = defineProps(["formVisible", "formData", "type"]);
const emits = defineEmits(["changeVisible", "submit"]);

// 表单数据
const form = reactive({
  id: props.formData.id,
  name: props.formData.name,
  tel: props.formData.tel,
  tId: props.formData.team,
  power: props.formData.power
})

// 侦听表单提交状态（添加）和弹出框的显示状态
watchEffect(()=>{
  if(props.type === 0 && props.formVisible){
    // 如果弹出框显示，且状态为添加，清空表单数据
    form.name = "";
    form.tel = "";
    form.tId = "";
    form.power = "";
  }
})
// 侦听表单数据
watch(()=>props.formData, (v)=>{
  // 根据父组件传入的最新数据，更新表单内容
  form.id = v.id;
  form.name = v.name;
  form.tel = v.tel;
  form.tId = v.tId;
  form.power = v.power;
},{
  deep: true
})

const formLabelWidth = "140px";

// 弹出框的显示隐藏状态
const dialogFormVisible = ref(false);
// 根据父组件传入的状态切换
watch(()=>props.formVisible, (v)=>{
  if(!v) return;
  dialogFormVisible.value = v;
});
// 关闭弹出框时要执行的功能：通知父组件最新状态
function closeFn(){
  emits("changeVisible", false);
}

// 点击表单提交按钮时的功能
const submitFn = function(){
  if(!form.name || !form.tel || form.power==="" || !form.tId){
    return;
  }
  dialogFormVisible.value = false;
  // 执行父组件传入的提交功能（添加或修改或其他）
  // 同时将表单数据发送给父组件
  emits("submit", form);
}


const team = reactive([]);
getTeam({
  token: userStore.token
}).then(res=>{
  res.data.data.forEach(val=>{
    team.push({
      tId: val.tId,
      tName: val.tName,
      tLeader: val.tLeader
    })
  })
})
</script>

<style scoped>
.el-button--text {
  margin-right: 15px;
}
.el-select {
  width: 300px;
}
.el-input {
  width: 300px;
}
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
